package me.numark.activities;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.umeng.analytics.MobclickAgent;

import java.util.LinkedHashMap;
import java.util.Map;

import me.numark.R;
import me.numark.fragments.ChatFragment;
import me.numark.model.User;
import me.numark.net.NetworkConstants;
import me.numark.providers.Numa;
import me.numark.utils.Global;
import me.numark.utils.HttpUtil;


public class ChatActivity extends BaseActivity {

    public static final String EXTRA_DID = "did";
    public static final String EXTRA_USER_UID = "uid";
    public static final String EXTRA_USER_AVATAR = "pic";
    public static final String EXTRA_USER_NICK = "nick";
    public int uid;
    long did;

    public static void startChat(Context context, User user) {
        Intent intent = new Intent(context, ChatActivity.class);
        intent.putExtra(EXTRA_USER_UID, user.uid);
        intent.putExtra(EXTRA_USER_AVATAR, user.pic);
        intent.putExtra(EXTRA_USER_NICK, user.nick);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);
        Intent intent = getIntent();
        uid = intent.getIntExtra(EXTRA_USER_UID, 0);
        did = intent.getLongExtra(EXTRA_DID, 0);
        String pic = intent.getStringExtra(EXTRA_USER_AVATAR);
        String nick = intent.getStringExtra(EXTRA_USER_NICK);
        ChatFragment fragment = ChatFragment.newInstance(uid, pic, nick, did);
        getSupportFragmentManager().beginTransaction().add(R.id.fragment, fragment).commitAllowingStateLoss();
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
        Global.currentChatUid = uid;
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
        Global.currentChatUid = 0;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Global.currentChatUid = 0;
        clearBadge();
    }

    protected void clearBadge() {
        if (did == 0) {
            return;
        }
        /*
        boolean needClear = true;
        Cursor c = getContentResolver().query(Numa.DialogColumns.CONTENT_URI,
                new String[]{"badge"},
                "_id=" + did, null, null);
        if (c != null) {
            if (c.moveToFirst()) {
                if (c.getInt(0) != 0)
                    needClear = true;
                else
                    needClear = false;
            }
            c.close();
        }
        */
        ContentValues values = new ContentValues();
        values.put("badge", 0);
        getContentResolver().update(Numa.DialogColumns.CONTENT_URI, values, "_id=" + did, null);
        sendRead();
    }

    protected void sendRead() {
        final String url = String.format(NetworkConstants.MSG_READ_URL, Global.getCurrentUid());
        final Map<String, Object> params = new LinkedHashMap<>();
        params.put("did", String.valueOf(did));
        new Thread() {
            @Override
            public synchronized void start() {
                HttpUtil.get(url, params);
            }
        }.start();
    }
}
